package com.she.olympic;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.app.IntentService;
import android.app.PendingIntent;
import android.content.Intent;

public class C2DMRegService extends IntentService {
	// Log Tag
	private static final String TAG = C2DMRegService.class.getSimpleName();
	//****** REMEMBER TO MODIFY WITH CORRECT NAME  package name + REGISTER
	public static final String REGISTER_ACTION = "com.she.olympic.REGISTER";
	//****** REMEMBER TO MODIFY WITH CORRECT NAME  package name + UNREGISTER
	public static final String UNREGISTER_ACTION = "com.she.olympic.UNREGISTER";
	//
	private String mGMail;

	public C2DMRegService() {
		super(TAG);
		// TODO Auto-generated constructor stub
//		AccountManager manager = AccountManager.get(getApplicationContext());
//		Account[] accounts = manager.getAccountsByType("com.google");
//		//
//		mGMail = accounts[0].name;
	}

	@Override
	protected void onHandleIntent(Intent intent) {
		// TODO Auto-generated method stub
		
		if (REGISTER_ACTION.equals(intent.getAction())){ // register for C2DM
			registerForC2DM();
		}else if(UNREGISTER_ACTION.equals(intent.getAction())){ // unregister for C2DM
			unregisterForC2DM();
		}
	}

	private void registerForC2DM(){
//		mGMail = ((Olympic)getApplicationContext()).getGmail();
//		mGMail = "orange@kland.com.tw";
		mGMail = "orangeshe@gmail.com";
        Intent registrationIntent = new Intent("com.google.android.c2dm.intent.REGISTER");
        registrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
        registrationIntent.putExtra("sender", mGMail);
        super.startService(registrationIntent);		
	}
	
	private void unregisterForC2DM(){
        Intent unregistrationIntent = new Intent("com.google.android.c2dm.intent.UNREGISTER");
        unregistrationIntent.putExtra("app", PendingIntent.getBroadcast(this, 0, new Intent(), 0));
        super.startService(unregistrationIntent);
	}
}
